package me.green.ui.dialog;

import java.awt.*;
import java.sql.Timestamp;
import java.util.Date;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import me.green.bean.Plan;
import me.green.dao.PlanDao;
import me.green.ui.LocalCache;
import me.green.ui.global.UIResources;
import me.green.ui.global.UIStyle;
import me.green.ui.event.ShortcutKeyListener;
import com.toedter.calendar.JDateChooser;

/**
 * 计划窗口
 *
 * @author green
 */
public class PlanDialog extends JDialog {

    private static final long serialVersionUID = 1L;
    private JPanel jContentPane = null;

    private JLabel labDate = null;
    private JDateChooser txtDate = null;

    private JLabel labDetail = null;
    private JScrollPane jScrollPane = null;
    private JTextArea txtDetail = null;

    private JButton btnAdd = null;
    private JButton btnClear = null;


    /**
     * 截至日期组件初始化
     *
     * @return javax.swing.JTextField
     */
    private JDateChooser getTxtDate() {
        if (txtDate == null) {
            txtDate = new JDateChooser();
            txtDate.setBounds(new Rectangle(95, 10, 214, UIStyle.INPUT_HEIGHT));
            txtDate.setFont(UIStyle.defaultFont());
            txtDate.setDate(new Date());
        }
        return txtDate;
    }


    /**
     * This method initializes txtDetail
     *
     * @return javax.swing.JTextArea
     */
    private JTextArea getTxtDetail() {
        if (txtDetail == null) {
            txtDetail = new JTextArea();
            txtDetail.setFont(UIStyle.defaultFont());
            txtDetail.setMargin(new Insets(5, 5, 5, 5));
            txtDetail.requestFocus(true);
        }
        return txtDetail;
    }

    /**
     * This method initializes jScrollPane
     *
     * @return javax.swing.JScrollPane
     */
    private JScrollPane getJScrollPane() {
        if (jScrollPane == null) {
            jScrollPane = new JScrollPane();
            jScrollPane.setBounds(new Rectangle(95, 70, 660, 110));
            jScrollPane.setViewportView(getTxtDetail());
        }
        return jScrollPane;
    }

    /**
     * This method initializes btnAdd
     *
     * @return javax.swing.JButton
     */
    private JButton getBtnAdd() {
        if (btnAdd == null) {
            btnAdd = new JButton();
            btnAdd.setBounds(new Rectangle(95, 200, 70, 36));
            btnAdd.setFont(UIStyle.defaultFont());
            btnAdd.setText(UIStyle.text("save"));
            btnAdd.addActionListener(new java.awt.event.ActionListener() {
                @Override
                public void actionPerformed(java.awt.event.ActionEvent e) {
                    addNewPlan();
                }
            });
        }
        return btnAdd;
    }

    /**
     * This method initializes btnClear
     *
     * @return javax.swing.JButton
     */
    private JButton getBtnClear() {
        if (btnClear == null) {
            btnClear = new JButton();
            btnClear.setBounds(new Rectangle(185, 200, 70, 36));
            btnClear.setFont(UIStyle.defaultFont());
            btnClear.setText(UIStyle.text("close"));
            btnClear.addActionListener(new java.awt.event.ActionListener() {
                @Override
                public void actionPerformed(java.awt.event.ActionEvent e) {
                    PlanDialog.this.setVisible(false);
                    PlanDialog.this.dispose();
                }
            });
        }
        return btnClear;
    }


    /**
     * @param owner
     */
    public PlanDialog(Frame owner, boolean modal) {
        super(owner, modal);
        initialize();
        this.setLocationRelativeTo(owner);
        this.setVisible(true);
    }

    /**
     * This method initializes this
     *
     * @return void
     */
    private void initialize() {
        this.setSize(800, 320);
        this.setResizable(false);
        this.setTitle(UIStyle.text("title.add-plan"));
        this.setIconImage(new ImageIcon(this.getClass().getResource(UIResources.ICON_TRAY)).getImage());
        this.setContentPane(getJContentPane());
        this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        toolkit.addAWTEventListener(ShortcutKeyListener.getInstance(this), AWTEvent.KEY_EVENT_MASK);
    }

    /**
     * This method initializes jContentPane
     *
     * @return javax.swing.JPanel
     */
    private JPanel getJContentPane() {
        if (jContentPane == null) {
            labDate = new JLabel();
            labDate.setBounds(new Rectangle(10, 10, 100, UIStyle.INPUT_HEIGHT));
            labDate.setFont(UIStyle.defaultFont());
            labDate.setText(UIStyle.text("label.plan-date"));

            labDetail = new JLabel();
            labDetail.setBounds(new Rectangle(10, 70, 100, UIStyle.INPUT_HEIGHT));
            labDetail.setFont(UIStyle.defaultFont());
            labDetail.setText(UIStyle.text("label.plan-detail"));

            jContentPane = new JPanel();
            jContentPane.setLayout(null);
            jContentPane.add(labDate, null);
            jContentPane.add(getTxtDate(), null);

            jContentPane.add(labDetail, null);
            jContentPane.add(getJScrollPane(), null);

            jContentPane.add(getBtnAdd(), null);
            jContentPane.add(getBtnClear(), null);
        }
        return jContentPane;
    }

    /**
     * 添加计划
     */
    public void addNewPlan() {
        String detail = txtDetail.getText().trim();
        java.util.Date planDate = txtDate.getDate();
        if (planDate == null) {
            JOptionPane.showMessageDialog(this, UIStyle.text("error.wrong-date"),
                    UIStyle.text("tip"), 0);
            txtDate.cleanup();
            txtDate.requestFocus();
            return;
        }
        if (planDate.before(new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000))) {
            JOptionPane.showMessageDialog(this, UIStyle.text("tip.choose-future-date"),
                    UIStyle.text("tip"), 0);
            return;
        }

        Timestamp planDateTimestamp = new Timestamp(planDate.getTime());
        Timestamp addDate = new Timestamp(System.currentTimeMillis());

        Plan plan = new Plan();
        plan.setDetail(detail);
        plan.setPlanDate(planDateTimestamp);
        plan.setAddDate(addDate);
        plan.setUid(LocalCache.getUser().getId());

        boolean flag = new PlanDao().save(plan);
        if (flag) {
            JOptionPane.showMessageDialog(PlanDialog.this, UIStyle.text("success.save"));
            LocalCache.getFrameApp().updateEditorPaneContent();
            PlanDialog.this.setVisible(false);
            PlanDialog.this.dispose();
        } else {
            JOptionPane.showMessageDialog(PlanDialog.this,
                    UIStyle.text("error.save"), UIStyle.text("tip"), 0);
        }
    }

}
